﻿using fuzhu;
using SqlSugar;
using System.Runtime.CompilerServices;
using U8COWeb;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.AS;
using ZDEntity;

namespace Yonyou.U8.BO
{
    /// <summary>
    ///不能直接继承，因为调用co时，根据AS_Department类直接转换
    /// </summary>
    public class PositionVM
    {
        //当前的数据库连接字符串
        public string conStr { get; set; }
        public string xmlConfig { get; set; }
        public static string cDatabase = "";

        public AS_Position entity { get; set; }

        public PositionVM()
        {
            //conStr
            conStr = CanShu.conStr;
            //初始化定义xmlconfig
            xmlConfig = XmlOperation.GetConfigXML();
        }

       
        /// <summary>
        /// 查询货位是否存在，返回列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RetModel2 GetEntityByWhPosCode(string cwhcode, string cposcode)
        {
            try
            {
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {

                    Position pos = db.Queryable<Position>().Where(x => x.cWhCode == cwhcode && (x.cPosCode == cposcode || x.cPosName == cposcode)).First();
                    if (pos != null)
                    {
                        if (!pos.bPosEnd)
                        {
                            return DBService.ReErrObject("不是末级货位，无法选用！");
                        }
                        else
                        {
                            List<Position> lst = new List<Position> { pos };
                            return DBService.ReOKObject(lst);
                        }
                    }
                    else
                    {
                        return DBService.ReErrObject("查询不到货位！");
                    }
                }
            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        
        }

        /// <summary>
        /// 导入U8，新增或修改
        /// </summary>
        /// <returns></returns>
        public RetModel2 Import()
        {

            try
            {
                //设置默认只
                //this.entity.cWhValueStyle = "全月平均法";
                //this.entity.bMRP = true;
                //this.entity.iWHProperty = 0;
                //this.entity.bShop = false;
                //this.entity.bControlSerial = false;
                //this.entity.bInCost = true;
                //this.entity.bInAvailCalcu = true;
                //this.entity.bProxyWh = false;
                //检查是否存在这个编码
                bool binv = Exists(entity.cPosCode);

                if (!binv)
                {
                    entity.bPosEnd = true;
                    return this.Add();
                }
                else
                {
                    return this.Update();
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }



        }
        public RetModel2 Add()
        {
            try
            {
                string cerr;
                string cid;

                //末级编码系统自动处理了
                if (PositionDALCO.AddPosition(this.entity, xmlConfig, out cerr, out cid))
                {
                    //更新是否末级货位
                    using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                    {
                        ConnectionString = conStr,
                        DbType = SqlSugar.DbType.SqlServer,
                        IsAutoCloseConnection = true
                    }))
                    {
                        //先锋特定，货位级别2-3-4
                        string cUpPosCode = "";
                        if (this.entity.cPosCode.Length==5)
                        {
                             cUpPosCode = this.entity.cPosCode.Substring(0, 2); 

                        }
                        else if (this.entity.cPosCode.Length == 9)
                        {
                            cUpPosCode = this.entity.cPosCode.Substring(0, 5);
                        }
                        db.Updateable<Position>()
                                .SetColumns(x => x.bPosEnd == false)
                                .Where(x => x.cPosCode == cUpPosCode)
                                .ExecuteCommand();

                    }

                        return DBService.ReOKObject("新增完成");
                }
                else
                {
                    return DBService.ReErrObject(cerr);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }

        /// <summary>
        ///  变更
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public RetModel2 Update()
        {
            try
            {
                string cerr;
                string cid;
                if (PositionDALCO.ModifyPosition(this.entity, xmlConfig, out cerr))
                {

                    return DBService.ReOKObject("修改完成");
                }
                else
                {
                    return DBService.ReErrObject(cerr);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }

        }

        public RetModel2 Delete()
        {
            try
            {
                string cerr;
                string cid;

                if (PositionDALCO.DelPosition(entity.cPosCode, xmlConfig, out cerr))
                {
                    return DBService.ReOKObject("删除完成");
                }
                else
                {
                    return DBService.ReErrObject(cerr);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }
        /// <summary>
        /// 检查仓库是否存在，返回U8仓库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Position GetByCode()
        {
            if (entity.cWhCode == null)
            {
                return null;
            }

            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {

                Position Computation = db.Queryable<Position>().Where(x => x.cPosCode == entity.cPosCode).First();
                return Computation;
            }

        }
        /// <summary>
        /// 检查计量单位是否存在
        /// </summary>
        /// <param name="cCode"></param>
        /// <returns></returns>
        public bool Exists(string cCode)
        {

            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = CanShu.conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            { 
                var cus = db.Queryable<Position>().Where(x => x.cPosCode == cCode).First();
                if (cus == null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
                 
            }
        }


        public void MergeEntity()
        {
            AS_Position entity2 = new AS_Position();
            //取默认值合并
            entity = ASService.MergeEntity<AS_Position>(entity, entity2);
        }
    }
}
